package scau.erps.view.business.team;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

import scau.erps.app.Erps;
import scau.erps.entity.Balancesheet;
import scau.erps.entity.Sand;
import scau.erps.service.BalancesheetService;
import scau.erps.service.ProductService;
import scau.erps.service.SandService;
import scau.erps.service.TeamOrderService;
import scau.erps.service.impl.BalancesheetServiceImpl;
import scau.erps.service.impl.ProductServiceImpl;
import scau.erps.service.impl.SandServiceImpl;
import scau.erps.service.impl.TeamOrderServiceImpl;

/**
 * 资产负债表 * 
 * @author FYJ
 * 
 */
public class MyAssetsPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	protected JComboBox<String> teamCombo;
	protected JButton searchBtn, operationBtn1, operationBtn2, operationBtn3,
			operationBtn4;
	protected JPanel contentPanel, actionPanel;
	protected JScrollPane dataPanel;
	protected JTable table;
	protected DefaultTableCellRenderer render;
	private OrderTableModel3 dataModel;
	final int team_Id = Erps.getCurrentTeam().getId();
	TeamOrderService teamOrderService = Erps.getIocInstance().get(
			TeamOrderServiceImpl.class);
	ProductService productService = Erps.getIocInstance().get(
			ProductServiceImpl.class);
	SandService sandService = Erps.getIocInstance().get(SandServiceImpl.class);

	public MyAssetsPanel() {
		teamCombo = new JComboBox<String>(new String[] { "所有年份", "Y0", "Y1",
				"Y2", "Y3", "Y4", "Y5", "Y6", "Y7" });
		searchBtn = new JButton("显示今年资产负债表");
		operationBtn1 = new JButton("查询选中年份资产负债表");
		searchBtn.addActionListener(this);
		operationBtn1.addActionListener(this);
		actionPanel = new JPanel(); // 顶部交互操作面板
		actionPanel.add(searchBtn);
		actionPanel.add(teamCombo);
		actionPanel.add(operationBtn1);
		table = new JTable();
		table.setPreferredScrollableViewportSize(new Dimension(930, 500));
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		dataPanel = new JScrollPane(table);
		dataPanel.setViewportView(table);
		render = new DefaultTableCellRenderer();
		render.setHorizontalAlignment(JLabel.CENTER);
		table.setDefaultRenderer(Object.class, render);
		contentPanel = new JPanel();
		contentPanel.setLayout(new BorderLayout());
		contentPanel.add(actionPanel, BorderLayout.NORTH);
		contentPanel.add(dataPanel, BorderLayout.CENTER);
		this.add(contentPanel);
	}

	private OrderTableModel3 getOrderTableModel() {
		Sand sand = sandService.getSandByTeam(team_Id);
		String y = sand.getTime().charAt(1) + "";
		dataModel = new OrderTableModel3(y);
		return dataModel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == searchBtn) {//查看当年资产负债表
				dataModel = getOrderTableModel();
				this.table.setModel(dataModel);
		} else if (e.getSource() == operationBtn1) {//查看所选年份资产负债表
			String year = null;
			String y = null;
			if (teamCombo.getSelectedIndex() != 0) {
				year = (String) teamCombo.getSelectedItem();
				y = year.charAt(1)+"";
				dataModel = new OrderTableModel3(y);
				this.table.setModel(dataModel);
			} else
				JOptionPane.showMessageDialog(null, "请选择需要查询年份", "提示",
						JOptionPane.INFORMATION_MESSAGE);
		}

	}

	class OrderTableModel3 extends AbstractTableModel {
		private static final long serialVersionUID = 1L;
		private String[] title = { "资产", "年初数", "期末数", "负债及所有者权益", "年初数", "期末数" };
		private Object[][] data = null;
		private int team_Id = Erps.getCurrentTeam().getId();
		private int temp = 24, temp1 = 20, temp2 = 14, temp3 = 0, temp4 = 2,
				temp5 = 3, temp6 = 6, temp7 = 20, temp8 = 6, temp9 = 52,
				temp10 = 43, temp11 = 40, temp12 = 50, temp13 = 12, temp14 = 4,
				temp15 = 0, temp16 = 7, temp17 = 52, temp18 = 61, temp19 = 104,
				temp20 = 104;// 缓存去年资产负债表数据
		private int option = 0, option1 = 0, option2 = 0, option3 = 0,
				option4 = 0, option5 = 0, option6 = 0, option7 = 0,
				option8 = 0, option9 = 0, option10 = 0, option11 = 0,
				option12 = 0, option13 = 0, option14 = 0, option15 = 0,
				option16 = 0, option17 = 0, option18 = 0, option19 = 0,
				option20 = 0;// 当年资产负债表的数据
		BalancesheetService balancesheetService = Erps.getIocInstance().get(
				BalancesheetServiceImpl.class);

		public OrderTableModel3(String y) {
			List<Balancesheet> thisBalancesheetList = balancesheetService
					.getBalancesheetOfThisYear(team_Id, "Y" + y);
			Balancesheet thisBalancesheet = thisBalancesheetList.get(0);
			if (y.equals("0")) 
				y = "9";
				List<Balancesheet> oldBalancesheetList = balancesheetService
						.getBalancesheetOfLastYear(team_Id, "Y" + y);
				Balancesheet oldBalancesheet = oldBalancesheetList.get(0);
				temp = oldBalancesheet.getCash();
				temp1 = oldBalancesheet.getShort_termLoan();
				temp2 = oldBalancesheet.getReceivable();
				temp3 = oldBalancesheet.getPayable();
				temp4 = oldBalancesheet.getMaterial();
				temp5 = oldBalancesheet.getTax();
				temp6 = oldBalancesheet.getFinishedProduct();
				temp7 = oldBalancesheet.getLong_termLoan();
				temp8 = oldBalancesheet.getProductInProcess();
				temp9 = oldBalancesheet.getTotalCurrentAsset();
				temp10 = oldBalancesheet.getTotalLoan();
				temp11 = oldBalancesheet.getFactory();
				temp12 = oldBalancesheet.getCapital();
				temp13 = oldBalancesheet.getLine();
				temp14 = oldBalancesheet.getTotal_oldProfit();
				temp15 = oldBalancesheet.getLineInProcess();
				temp16 = oldBalancesheet.getNetProfit();
				temp17 = oldBalancesheet.getTotalFixedAsset();
				temp18 = oldBalancesheet.getTotalEquity();
				temp19 = oldBalancesheet.getTotalAsset();
				temp20 = oldBalancesheet.getTotal_equityAndLoan();
			// 读取去年数据作为年初数据
			option = thisBalancesheet.getCash();// 获取现金
			// 短期负债
			option1 = thisBalancesheet.getShort_termLoan();
			// 应收款
			option2 = thisBalancesheet.getReceivable();
			// 应付账款
			option3 = thisBalancesheet.getPayable();
			option4 = thisBalancesheet.getMaterial();// 获取原材料数
			option5 = thisBalancesheet.getTax();// 当年应交税金
			option6 = thisBalancesheet.getFinishedProduct();// 成品数量
			// 长期负债
			option7 = thisBalancesheet.getLong_termLoan();
			option8 = thisBalancesheet.getProductInProcess();// 在制品数量
			option9 = thisBalancesheet.getTotalCurrentAsset();// 流动资产合计
			option10 = thisBalancesheet.getTotalLoan();// 负债合计
			// 获取土地建筑
			option11 = thisBalancesheet.getFactory();
			option12 = thisBalancesheet.getCapital();// 股东资本固定为 50
			// 获取已有生产线数量
			option13 = thisBalancesheet.getLine();
			// 获取在建生产线数量
			option15 = thisBalancesheet.getLineInProcess();
			option14 = thisBalancesheet.getTotal_oldProfit();// 以前年度利润
			option16 = thisBalancesheet.getNetProfit();// 重构MyProfitLossPanel代码，直接获取表中netProfit的数据
			option17 = thisBalancesheet.getTotalFixedAsset();// 固定资产合计
			option18 = thisBalancesheet.getTotalEquity();// 所有者权益合计
			option19 = thisBalancesheet.getTotalAsset();// 资产总计
			option20 = thisBalancesheet.getTotal_equityAndLoan();// 负债及权益合计

			data = new Object[13][title.length];
			data[0][0] = "流动资产";
			data[0][3] = "负债";
			data[1][0] = "现金";
			data[1][1] = temp;
			data[1][2] = option;
			data[1][3] = "短期负债";
			data[1][4] = temp1;
			data[1][5] = option1;
			data[2][0] = "应收账款";
			data[2][1] = temp2;
			data[2][2] = option2;
			data[2][3] = "应付账款";
			data[2][4] = temp3;
			data[2][5] = option3;
			data[3][0] = "原材料";
			data[3][1] = temp4;
			data[3][2] = option4;
			data[3][3] = "应交税金";
			data[3][4] = temp5;
			data[3][5] = option5;
			data[4][0] = "产成品";
			data[4][1] = temp6;
			data[4][2] = option6;
			data[4][3] = "长期负债";
			data[4][4] = temp7;
			data[4][5] = option7;
			data[5][0] = "在制品";
			data[5][1] = temp8;
			data[5][2] = option8;
			data[6][0] = "流动资产合计";
			data[6][1] = temp9;
			data[6][2] = option9;
			data[6][3] = "负债合计";
			data[6][4] = temp10;
			data[6][5] = option10;
			data[7][0] = "固定资产";
			data[7][3] = "所有者权益";
			data[8][0] = "土地建筑";
			data[8][1] = temp11;
			data[8][2] = option11;
			data[8][3] = "股东资本";
			data[8][4] = temp12;
			data[8][5] = option12;
			data[9][0] = "机器设备";
			data[9][1] = temp13;
			data[9][2] = option13;
			data[9][3] = "以前年度利润";
			data[9][4] = temp14;
			data[9][5] = option14;
			data[10][0] = "在建工程";
			data[10][1] = temp15;
			data[10][2] = option15;
			data[10][3] = "当年净利润";
			data[10][4] = temp16;
			data[10][5] = option16;
			data[11][0] = "固定资产合计";
			data[11][1] = temp17;
			data[11][2] = option17;
			data[11][3] = "所有者权益合计";
			data[11][4] = temp18;
			data[11][5] = option18;
			data[12][0] = "资产总计";
			data[12][1] = temp19;
			data[12][2] = option19;
			data[12][3] = "负债及权益合计";
			data[12][4] = temp20;
			data[12][5] = option20;//显示资产负债表

		}

		@Override
		public int getRowCount() {
			return this.data.length; // 行数
		}

		@Override
		public int getColumnCount() {
			return this.title.length; // 列数
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			return data[rowIndex][columnIndex]; // 取表格单元格值
		}

		public String getColumnName(int columnIndex) {
			return title[columnIndex]; // 显示标题
		}

	}
}
